package com.ahucoding.rocket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ahucoding.rocket.common.model.Post;
import com.ahucoding.rocket.common.param.PostParam;
import com.ahucoding.rocket.common.mapper.PostMapper;
import com.ahucoding.rocket.common.utils.MyEntityTool;
import com.ahucoding.rocket.service.PostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ahucoding.rocket.common.constants.EnvironmentConstants;
import com.ahucoding.rocket.common.utils.DeleteFlagEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import lombok.SneakyThrows;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.time.LocalDateTime;

/**
 * @Description 内容表 Service实现
 * @Author
 * @CreateDate
 * @Version
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    /**
     * 内容表新增
     *
     * @param postParam PostParam
     */
    @SneakyThrows
    @Override
    public void createPost(PostParam postParam) {
        Post post = new Post();
        BeanUtils.copyProperties(postParam, post);
        MyEntityTool.insertEntity(post);
        super.save(post);
    }

    /**
     * 内容表修改
     *
     * @param postParam PostParam
     */
    @SneakyThrows
    @Override
    public void updatePost(PostParam postParam) {
        Post post = new Post();
        BeanUtils.copyProperties(postParam, post);
        MyEntityTool.updateEntity(post);
        super.updateById(post);
    }

    /**
     * 根据id获取内容表信息
     *
     * @param id String
     * @return Post
     */
    @Override
    public Post getPostById(Long id) {
        return super.getById(id);
    }

    /**
     * 内容表分页列表
     *
     * @param currentPage  当前页
     * @param pageSize     页大小
     * @param order        排序方式
     * @param orderFields  排序字段
     * @param id           $column.comment
     * @param createBy     创建人
     * @param createDate   创建时间
     * @param updateBy     更新人
     * @param updateDate   更新时间
     * @param deleteFlag   数据状态 0可用 -1不可用
     * @param title        标题
     * @param name         名称
     * @param url          链接地址
     * @param content      内容
     * @param shortContent 内容总结
     * @param isJudge      是否经过评价
     * @param isUserDefine 是否用户定制
     * @param sourceId     源id
     * @param contentDate  内容发表时间
     * @param categoryId   类别id
     * @param stars        得分
     * @param ttl          语音文件地址
     * @return Page
     */
    @Override
    public Page page(Integer currentPage, Integer pageSize, String order, String orderFields
            , Long id
            , String createBy
            , LocalDateTime createDate
            , String updateBy
            , LocalDateTime updateDate
            , Integer deleteFlag
            , String title
            , String name
            , String url
            , String content
            , String shortContent
            , Integer isJudge
            , Integer isUserDefine
            , Long sourceId
            , String contentDate
            , Long categoryId
            , Integer stars
            , String ttl
    ) {
        Page<Post> page = new Page<>(currentPage, pageSize);
        if (EnvironmentConstants.ORDER_FIELDS_DESC.equals(order)) {
            page.addOrder(OrderItem.desc(orderFields));
        } else {
            page.addOrder(OrderItem.asc(orderFields));
        }
        LambdaQueryWrapper<Post> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Post::getDeleteFlag, DeleteFlagEnum.DATA_OK.getFlag());
        if (id != null) {
            wrapper.eq(Post::getId, id);
        }
        if (StringUtils.isNotEmpty(createBy)) {
            wrapper.eq(Post::getCreateBy, createBy);
        }
        if (createDate != null) {
            wrapper.eq(Post::getCreateDate, createDate);
        }
        if (StringUtils.isNotEmpty(updateBy)) {
            wrapper.eq(Post::getUpdateBy, updateBy);
        }
        if (updateDate != null) {
            wrapper.eq(Post::getUpdateDate, updateDate);
        }
        if (deleteFlag != null) {
            wrapper.eq(Post::getDeleteFlag, deleteFlag);
        }
        if (StringUtils.isNotEmpty(title)) {
            wrapper.eq(Post::getTitle, title);
        }
        if (StringUtils.isNotEmpty(name)) {
            wrapper.eq(Post::getName, name);
        }
        if (StringUtils.isNotEmpty(url)) {
            wrapper.eq(Post::getUrl, url);
        }
        if (StringUtils.isNotEmpty(content)) {
            wrapper.eq(Post::getContent, content);
        }
        if (StringUtils.isNotEmpty(shortContent)) {
            wrapper.eq(Post::getShortContent, shortContent);
        }
        if (isJudge != null) {
            wrapper.eq(Post::getIsJudge, isJudge);
        }
        if (isUserDefine != null) {
            wrapper.eq(Post::getIsUserDefine, isUserDefine);
        }
        if (sourceId != null) {
            wrapper.eq(Post::getSourceId, sourceId);
        }
        if (StringUtils.isNotEmpty(contentDate)) {
            wrapper.eq(Post::getContentDate, contentDate);
        }
        if (categoryId != null) {
            wrapper.eq(Post::getCategoryId, categoryId);
        }
        if (stars != null) {
            wrapper.eq(Post::getStars, stars);
        }
        if (StringUtils.isNotEmpty(ttl)) {
            wrapper.eq(Post::getTtl, ttl);
        }
        return super.page(page, wrapper);
    }

    /**
     * 内容表删除
     *
     * @param id String
     */
    @SneakyThrows
    @Override
    public void deletePost(String id) {
        List<String> idList = Arrays.asList(id.split(","));
        // 设置修改条件
        if (CollectionUtils.isEmpty(idList)) {
            throw new RuntimeException("请选择要删除的数据");
        }
        List<Long> ids = idList.stream().map(Long::parseLong).collect(Collectors.toList());
        super.removeBatchByIds(ids);
    }


}

